/**
 * Copyright (c) 2018-2025, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: ring buffer
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2025-06-10     JasonHu           Init
 */

#include <base/ringbuffer.h>
#include <base/malloc.h>
#include <base/math.h>
#include <base/debug.h>
#include <base/barrier.h>
#include <base/memory.h>

NX_Error NX_RingBufferInit(NX_RingBuffer * ring, NX_U8 * buffer, NX_Size size)
{
    if (!NX_IsPowerOf2(size))
    {
        return NX_EINVAL;
    }
    
    ring->buffer = buffer;
    ring->size = size;
    ring->in = ring->out = 0;
    return NX_EOK;
}

NX_RingBuffer * NX_RingBufferCreate(NX_Size size)
{
    NX_U8 * buffer;
    NX_RingBuffer * ring;

    if (!NX_IsPowerOf2(size))
    {
        NX_ASSERT(size < 0x80000000);
        size = NX_RoundupPowOf2(size);
    }

    buffer = NX_MemAlloc(size);
    if (buffer == NX_NULL)
    {
        return NX_NULL;
    }

    ring = NX_MemAlloc(sizeof(NX_RingBuffer));
    if (ring == NX_NULL)
    {
        NX_MemFree(buffer);
        return NX_NULL;
    }
    
    if (NX_RingBufferInit(ring, buffer, size) != NX_EOK)
    {
        NX_MemFree(buffer);
        NX_MemFree(ring);
        ring = NX_NULL;
    }
    
    return ring;
}

void NX_RingBufferDestroy(NX_RingBuffer *ring)
{
    NX_MemFree(ring->buffer);
    NX_MemFree(ring);
}

NX_Size NX_RingBufferWrite(NX_RingBuffer * ring, const NX_U8 * buffer, NX_Size len)
{
    NX_Size minLen;

    len = NX_MIN(len, ring->size - ring->in + ring->out);
    NX_MemoryBarrier();
    minLen = NX_MIN(len, ring->size - (ring->in & (ring->size - 1)));
    NX_MemCopy((void *)(ring->buffer + (ring->in & (ring->size - 1))), buffer, minLen);
    NX_MemCopy((void *)ring->buffer, buffer + minLen, len - minLen);
    NX_MemoryBarrierWrite();
    ring->in += len;
    
    return len;
}

NX_Size NX_RingBufferRead(NX_RingBuffer * ring, const NX_U8 * buffer, NX_Size len)
{
    NX_Size minLen;

    len = NX_MIN(len, ring->in - ring->out);
    NX_MemoryBarrierRead();
    minLen = NX_MIN(len, ring->size - (ring->out & (ring->size - 1)));
    NX_MemCopy((void *)buffer, ring->buffer + (ring->out & (ring->size - 1)), minLen);
    NX_MemCopy((void *)(buffer + minLen), ring->buffer, len - minLen);
    NX_MemoryBarrier();
    ring->out += len;
    
    return len;
}

NX_Size NX_RingBufferPeek(NX_RingBuffer * ring, const NX_U8 * buffer, NX_Size len)
{
    NX_Size minLen;

    len = NX_MIN(len, ring->in - ring->out);
    NX_MemoryBarrierRead();
    minLen = NX_MIN(len, ring->size - (ring->out & (ring->size - 1)));
    NX_MemCopy((void *)buffer, ring->buffer + (ring->out & (ring->size - 1)), minLen);
    NX_MemCopy((void *)(buffer + minLen), ring->buffer, len - minLen);
    NX_MemoryBarrier();

    return len;
}

void NX_RingBufferReset(NX_RingBuffer *ring)
{
    ring->in = ring->out = 0;
}

NX_Size NX_RingBufferLen(NX_RingBuffer *ring)
{
    NX_Size len;
    len = ring->in - ring->out;
    return len;
}

NX_Size NX_RingBufferAvaliable(NX_RingBuffer *ring)
{
    NX_Size len;
    len = ring->size - (ring->in - ring->out);
    return len;
}
